<?php

class Admin_CategoryController extends Kieumau_Controller_Admin
{

    public function init()
    {
    	parent::init();
	}
    
	public function indexAction(){
		$this->_redirect('admin/category/list');
	}
	
	public function listAction(){
		
		$this->view->mainMenu = "Manage";
		$this->view->subMenu = "Category";
		$categoryTable = new Admin_Model_DbTable_Category();
		
		$categoryList = $categoryTable->getAllByTable();
		
		$page = $this->_getParam ( 'page', 1 );
		$paginator = Zend_Paginator::factory ( $categoryList );
		$paginator->setItemCountPerPage ( 10 );
		$paginator->setCurrentPageNumber ( $page );
		$this->view->categoryList = $paginator;
	}
	
  	public function addAction(){
    	$this->view->mainMenu = "Manage";
    	$this->view->subMenu = "Category";
    	$categoryTable = new Admin_Model_DbTable_Category();
    	
    	if($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		$foreign = $formData['foreign'];
    		unset($formData['foreign']);
    		
    		$validateParams = array('name'=>'setRequired');
    		$error = $this->formValidate($formData, $validateParams);
    		
    		// If there's no error, The system will save data into database
    		if(count($error)==0){
    			$id = $categoryTable->insertTable($formData);//Insert table

    			//Insert category_taxonomy
    			if( !empty ($foreign['taxonomy'])){
	    			$categorytaxonomyTable = new Admin_Model_DbTable_CategoryTaxonomy();
	    			$categorytaxonomyTable->insertData($id,$foreign['taxonomy']);
    			}
    			$this->_redirect($this->getRequest()->getModuleName() . '/' . $this->getRequest()->getControllerName());
    			
    		}else{//if exists error, fill form again and notice error
				$this->view->formData = $formData;
				$this->view->error = $error;
				
				$taxonomySelect  = $this->getTaxonomyAsDropdown($formData['foreign']['taxonomy']);
				$taxonomySelect = '<option value="">Select one</option>'.$taxonomySelect;
				$this->view->taxonomyList = $taxonomySelect;
			}
    	}else{
    		$taxonomySelect  = $this->getTaxonomyAsDropdown();
    		$taxonomySelect = '<option value="">Select one</option>'.$taxonomySelect;
    		$this->view->taxonomyList = $taxonomySelect;
    	}
    }
    
    public function editAction(){
    	$this->view->mainMenu = "Manage";
    	$this->view->subMenu = "Category";
    	
    	$id = $this->getRequest()->getParam('id',0);
    	$categoryTable = new Admin_Model_DbTable_Category();
    	
    	if($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		$foreign = $formData['foreign'];
    		unset($formData['foreign']);
    		
    		$validateParams = array('name'=>'setRequired');
    		$error = $this->formValidate($formData, $validateParams);
    		
    		// If there's no error, The system will save data into database
    		if(count($error)==0){
    			$categoryTable->updateTable($formData, (int)$id);//Update main table
    			
    			//Update foreign table
    				$categorytaxonomyTable = new Admin_Model_DbTable_CategoryTaxonomy();
    				$categorytaxonomyTable->updateTable($id,$foreign['taxonomy']);
    			
    			$this->_redirect($this->getRequest()->getModuleName() . '/' . $this->getRequest()->getControllerName());
    		}else{//if exists error, fill form again and notice error
    			$this->view->formData = $formData;
    			$this->view->error = $error;
    		}
    	}else{
    		$formData = $categoryTable->getAllByTable('id = '.(int)$id);
    		$this->view->formData = $formData[0];
    		
    		$commonTable = new Kieumau_Db_Common();
    		$taxonomyId = $commonTable->getColumn('category_taxonomy',array('taxonomy_id'), 'category_id = ?',array($id) );
    		
    		if(!empty($taxonomyId))  
    			$taxonomyId = $taxonomyId[0]['taxonomy_id'];
    		
    		$taxonomySelect  = $this->getTaxonomyAsDropdown( $taxonomyId );
    		$taxonomySelect = '<option value="">Select one</option>'.$taxonomySelect;
    		$this->view->taxonomyList = $taxonomySelect;
    	}
    }
    
    public function deleteAction(){
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender ();
    	
    	if($this->getRequest()->isPost()){
    		$id = $this->getRequest()->getPost('id');
    		$categoryTable = new Admin_Model_DbTable_Category();
    		$categoryTable->deleteTableLanguage($id); //Delete in category Table
    		
    		
    		//Delete in category_taxonomy Table
    		$categorytaxonomyTable = new Admin_Model_DbTable_CategoryTaxonomy();
    		$categorytaxonomyTable->updateTable($id,'');
    		
    	}
    }
    
    public function activateAction(){
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender ();
    	if($this->getRequest()->isPost()){
    		$id = $this->getRequest()->getPost('id');
    		$categoryTable = new Admin_Model_DbTable_Category();
    		$categoryTable->activate($id);
    	}
    }
    
    public function detailAction(){
    	$this->view->mainMenu 	= "Manage";
    	$this->view->subMenu 	= "Category";
    	 
    	$categoryId = $this->getRequest()->getParam('id');
    	 
    	$categoryTable = new Admin_Model_DbTable_Category();
    	 
    	//Get detail of this taxonomy
    	$categoryInfo = $categoryTable->getAllByTable('id = '.$categoryId);
    	
    	//Get Page inside this taxonomy
    	$pageCategoryTable = new Admin_Model_DbTable_PageCategory();
    	$pageList = $pageCategoryTable->getPageChildren($categoryId, $this->defaultLanguageId);
    	
    	//Assign to view
    	$this->view->categoryInfo = $categoryInfo[0];
    	$this->view->pageList = $pageList;
    }
    
    //return as String
    public function getTaxonomyAsDropdown($selectedValue = ''){
    	 
    	$languageTableContent = $this->language->getLanguageDefault();//Get default Language Row
    	 
    	$params = array('tableName'=>'taxonomy_language',//Tên table
    			'where'=>'lang_id = ? AND `taxonomy_id` in (SELECT id FROM taxonomy WHERE enabled = 1)',//Điều kiện lọc
    			'params'=>array($languageTableContent['id']),//Tham số truyền vào cho where
    			'value'=>'taxonomy_id',//Trường muốn làm value, trong select
    			'text'=>'name'//Trường muốn làm text, trong select
    	);
    	 
    	return $this->createSelectCtrller($params , $selectedValue);
    }
}